import { pageTo, toast } from 'nc-lightapp-front'
import config from '../config'
import api from '../../../common/api'

// props, moduleId(区域id), key(操作的键), value（当前值）, changedrows（新旧值集合）, index（当前index）, record（行数据）,type(表格内为line，弹窗为modal), method(有blur有change)
export default function (props, moduleId, key, value, changedrows) {
	
	switch (moduleId) {
		case config.headcode:
			let headLinkAttrs = ['bx_pcorg' /*利润中心 */, , 'pk_pcorg_v' /*利润中心版本 */, 'pk_resacostcenter', 'fydwbm']
			if (key === 'pk_org') {
				if(!value||!value.value){
					
					props.form.setFormItemsValue('head',{'pk_org': changedrows})
					return
				}
			}
			if (key === 'pk_org_v') {
				if(!value||!value.value){
					const obj = {
						display:changedrows.display,value:changedrows.value
					}
					props.form.setFormItemsValue('head',{'pk_org_v': changedrows})
					return
				}
			}
			if (headLinkAttrs.indexOf(key) > -1) {
				
				let paramdata = {}
				paramdata = {
					value: value,
					tableid: 'headform',
					field: key,
					uistate: pageTo.getUrlParam('status'),
					tradetype: pageTo.getUrlParam('tradetype'),
					oldvalue: '',
					pagecode: config.pagecode,
					appcode: config.appcode
				}
				let billdata = props.createHeadAfterEventData(
					config.pagecode,
					config.headcode,
					[config.bodycode],
					moduleId,
					key,
					value
				)
				let send_headdata = { billdata, paramdata }
				send_headdata.templateid = props.meta.getMeta().pageid
				api.valueChange(send_headdata).then(data => {
					//设置表头数据
					props.form.setAllFormValue({ [config.headcode]: data.head[config.headcode] })
					//设置表体数据
					// props.cardTable.setTableData(config.bodycode, data.bodys[config.bodycode]);
					//改为更新数据，setTableData会把页面假删除的数据覆盖掉
					// props.cardTable.updateDataByRowId(config.bodycode, data.bodys[config.bodycode])
				})
			}
			break

		case config.bodycode:
			console.log('=====OnTableValueChange:', props, moduleId, key, value, changedrows)
			let paramdata = {}
			paramdata = {
				value: value,
				tableid: moduleId,
				field: key,
				uistate: pageTo.getUrlParam('status'),
				tradetype: pageTo.getUrlParam('tradetype'),
				oldvalue: '',
				pagecode: config.pagecode,
				appcode: config.appcode
			}
			paramdata.changeRowId = changedrows[0].rowid

			if (changedrows[0].newvalue.value == changedrows[0].oldvalue.value) {
				return
			}
			let billdata = props.createHeadAfterEventData(
				config.pagecode,
				config.headcode,
				[config.bodycode],
				moduleId,
				key,
				value
			)
			for (let bodyArea in billdata.card.bodys) {
				//只发送未删除的行
				billdata.card.bodys[bodyArea].rows = props.cardTable.getVisibleRows(bodyArea)
				if(key==="assume_org"){
					billdata.card.bodys[bodyArea].rows[0].values.assume_dept = {
						display: "",
						value: "",
						_display_: "",
					}
				}
			}
			
			console.log('表头字段change后表头标题数据结构：', billdata)
			if (value.value == undefined || value.value == null) {
				paramdata.value = changedrows[0].newvalue.value
			}

			let send_headdata = { billdata, paramdata }
			send_headdata.templateid = props.meta.getMeta().pageid
			api.valueChange(send_headdata).then(data => {
				//设置表头数据
				props.form.setAllFormValue({ [config.headcode]: data.head[config.headcode] })
				//设置表体数据
				// props.cardTable.setTableData(config.bodycode, data.bodys[config.bodycode]);
				//改为更新数据，setTableData会把页面假删除的数据覆盖掉
				props.cardTable.updateDataByRowId(config.bodycode, data.bodys[config.bodycode])
			})
	}
}
